﻿using System.Threading.Tasks;
using CkSoftware.GroupMe.Sdk.Bot.Commands;
using CkSoftware.GroupMe.Sdk.Core.Dao;

namespace CkSoftware.GroupMe.Sdk.Bot
{
	/// <summary>
	/// The bot send-message managing class responsible for submitting bot-message with or without attachments.
	/// </summary>
	public class GroupMeBotSendMessage
	{
		private readonly GroupMeBot _bot;
		private readonly PostBotMessageCommand _command;

		/// <summary>
		/// Instantiates a new <see cref="GroupMeBotSendMessage" /> class.
		/// </summary>
		/// <param name="bot">The bot for which the message should be sent.</param>
		/// <param name="message">The message content.</param>
		public GroupMeBotSendMessage(GroupMeBot bot, string message)
		{
			_bot = bot;
			_command = new PostBotMessageCommand(_bot.Data.Id, message);
		}

		/// <summary>
		/// Submits the message to the GroupMe API.
		/// </summary>
		public void Submit()
		{
			Task.WaitAll(_bot.SendMessageInternal(_command));
		}

		/// <summary>
		/// Submits the message to the GroupMe API asynchronous.
		/// </summary>
		/// <returns>An awaitable task.</returns>
		public async Task SubmitAsync()
		{
			await _bot.SendMessageInternal(_command);
		}

		/// <summary>
		/// Defines that the message should contain a attachment.
		/// </summary>
		/// <param name="attachment">The attachment to add to the bot-message.</param>
		/// <returns>The extended <see cref="GroupMeBotSendMessage" /> object.</returns>
		public GroupMeBotSendMessage WithAttachment(GroupMeAttachmentBase attachment)
		{
			_command.WithAttachment(attachment);
			return this;
		}
	}
}